package com.seeyon.apps.gkcwplugin.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SoapsendUtil {
	private static final Log logger=LogFactory.getLog(SoapsendUtil.class);
	public String postSoap(String soapXML,String url) {
		logger.info("---------------------SoapUtil-postSoap");
		try {
			StringBuilder sb = new StringBuilder();
			URL soapUrl=new URL(url);
			HttpURLConnection connection=(HttpURLConnection)soapUrl.openConnection();
			connection.setRequestMethod("POST");
			//connection.setRequestProperty("content-type", "application/soap+xml;charset=utf-8");
			connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
			getEncoding(soapXML);
			//connection.setRequestProperty("SOAPAction",soapActions);//"命名空间/方法名"
			//connection.setUseCaches(false);
			 //3.3设置输入输出，新创建的connection默认是没有读写权限的，
	        connection.setDoInput(true);
	        connection.setDoOutput(true);
	        //connection.setConnectTimeout(10000);
	       //connection.setReadTimeout(10000);
	        //4：组织SOAP协议数据，发送给服务端
	        OutputStream os = connection.getOutputStream();
	        os.write(soapXML.getBytes("utf-8"));
	        //os.write(soapXML.getBytes());
	      //5：接收服务端的响应
	        int responseCode = connection.getResponseCode();
	        logger.info("------------------SoapUtil-响应："+responseCode);
	        if(200 == responseCode){//表示服务端响应成功
	            InputStream is = connection.getInputStream();
	            InputStreamReader isr = new InputStreamReader(is,"utf-8");
	            BufferedReader br = new BufferedReader(isr);
	            String temp = null;
	            while(null != (temp = br.readLine())){
	                sb.append(temp);
	            }
	            is.close();
	            isr.close();
	            br.close();
	        }else {
				sb.append("f");//接口响应失败
			}
	        os.close();
	        return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (isEncoding(str, encode)) { // 判断是不是GB2312
            	logger.info("----------------编码："+encode);
                return encode;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (isEncoding(str, encode)) { // 判断是不是ISO-8859-1
            	logger.info("----------------编码："+encode);
                return encode;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (isEncoding(str, encode)) { // 判断是不是UTF-8
            	logger.info("----------------编码："+encode);
                return encode;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (isEncoding(str, encode)) { // 判断是不是GBK
            	logger.info("----------------编码："+encode);
                return encode;
            }
        } catch (Exception exception3) {
        }
        return ""; // 如果都不是，说明输入的内容不属于常见的编码格式。
    }
	
	public static boolean isEncoding(String str, String encode) {
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return true;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return false;
    }
}
